package com.gupaoedu.vip.netty.io.bio.tomcat.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @program:gupaoedu-vip-netty-io
 * @description:封装InputStream
 * @author:Gongchao
 * @date:2020-04-26
 **/
public class GPRequest {

    private String method;

    private String url;

    /* *
     * @description:从输入流中拿到http协议内容
     * @author:Gongchao
     * @date:2020-四月-26
     * @param:[inputStream]
     * @return:
     */
    public GPRequest (InputStream inputStream){

        String content = "";
        //创建内存输出流
        ByteArrayOutputStream byteArrayOutputStream = null;
        byte[] bytes = new byte[1024];
        int len = 0 ;
        try {
            if ((len = inputStream.read(bytes)) > 0) {
                byteArrayOutputStream = new ByteArrayOutputStream();
                byteArrayOutputStream.write(bytes,0,len);
                content = byteArrayOutputStream.toString("utf-8");
//                content = new String(bytes,0,len);

            }

            System.out.println(content);
            String line = content.split("\\n")[0];//  /n换行符
            String[] args = content.split("\\s"); //  \\s 作为分隔符表示空白

            this.method = args[0];
            this.url = args[1].split("\\?")[0];

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
